﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Project.DTO;
using System.Security.Cryptography;
using System.Text;

namespace Project
{
    public partial class Signup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lbMes.Text = "";            
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            string username = tbUsername.Text.Trim();
            string fullname = tbName.Text.Trim();
            string password = tbPassword.Text.Trim();
            string origin = "Local";
            
            //MD5 hash

            MD5 md5 = new MD5CryptoServiceProvider();
            md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(password));
            byte[] result = md5.Hash;
            StringBuilder strBuilder = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                strBuilder.Append(result[i].ToString("x2"));
            }

            password = strBuilder.ToString();
            //End MD5 hash

            userDTO usn = new userDTO();
            usn.userName = username;
            usn.fullName = fullname;
            usn.passWord = password;
            usn.origin = origin;


            Boolean rs = BUS.userBUS.insertNewUser(usn);
            if (rs)
            {
                //register successed
                lbMes.Text = "Register successed.";
            }
            else {
                //username is available
                tbUsername.Focus();
                lbMes.Text = "Username is available.";
                return;
            }

        }
    }
}